package com.pointstar.dp.modules.project.service.impl;

import com.pointstar.dp.common.entity.Page;
import com.pointstar.dp.common.entity.Query;
import com.pointstar.dp.common.entity.R;
import com.pointstar.dp.common.utils.CommonUtils;
import com.pointstar.dp.modules.project.dao.InBoundDetailsMapper;
import com.pointstar.dp.modules.project.dao.InOutBoundMapper;
import com.pointstar.dp.modules.project.dao.OutBoundDetailsMapper;
import com.pointstar.dp.modules.project.dao.OutBoundMapper;
import com.pointstar.dp.modules.project.entity.InBoundDetailsEntity;
import com.pointstar.dp.modules.project.entity.OutBoundDetailsEntity;
import com.pointstar.dp.modules.project.entity.OutBoundEntity;
import com.pointstar.dp.modules.project.entity.ro.SaveOutBoundRo;
import com.pointstar.dp.modules.project.service.OutBoundService;
import com.pointstar.dp.modules.project.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出库主表
 * @author Korey<m13057531285@163.com>
 */
@Service("outBoundService")
public class OutBoundServiceImpl implements OutBoundService {

	private final OutBoundMapper outBoundMapper;
	private final OutBoundDetailsMapper outBoundDetailsMapper;
	private final InBoundDetailsMapper inBoundDetailsMapper;
	private final InOutBoundMapper inOutBoundMapper;

	@Autowired
	public OutBoundServiceImpl(OutBoundMapper outBoundMapper, OutBoundDetailsMapper outBoundDetailsMapper, InBoundDetailsMapper inBoundDetailsMapper,InOutBoundMapper inOutBoundMapper) {
		this.outBoundMapper = outBoundMapper;
		this.outBoundDetailsMapper = outBoundDetailsMapper;
		this.inBoundDetailsMapper = inBoundDetailsMapper;
		this.inOutBoundMapper = inOutBoundMapper;
	}

	/**
     * 分页查询
     * @param params
     * @return
     */
	@Override
	public Page<OutBoundEntity> listOutBound(Map<String, Object> params) {
		Query query = new Query(CommonUtil.setTime(params));
		Page<OutBoundEntity> page = new Page<>(query);
		outBoundMapper.listForPage(page, query);
		return page;
	}

    /**
     * 新增
     * @param outBound
     * @return
     */
	@Override
	public R saveOutBound(OutBoundEntity outBound) {
		int count = outBoundMapper.save(outBound);
		return CommonUtils.msg(count);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R saveOutBoundAndDetails(SaveOutBoundRo saveOutBoundRo) {
		//插入出库主表
		BigDecimal totalMoney = new BigDecimal("0");
		for (OutBoundDetailsEntity outBoundDetailsEntity : saveOutBoundRo.getOutBoundDetailsEntities()) {
			totalMoney = totalMoney.add(outBoundDetailsEntity.getTotalPrice());
		}
		OutBoundEntity outBoundEntity = OutBoundEntity.builder()
				.outBoundNumber(saveOutBoundRo.getOutBoundNumber())
				.customer(saveOutBoundRo.getCustomer())
				.amount(totalMoney)
				.outTypeCode(saveOutBoundRo.getOutTypeCode())
				.outBoundPeople(saveOutBoundRo.getOutBoundPeople()).build();
		int countOutBound = outBoundMapper.save(outBoundEntity);
		InBoundDetailsEntity inBoundDetailsEntity;
		for (OutBoundDetailsEntity outBoundDetailsEntity : saveOutBoundRo.getOutBoundDetailsEntities()) {
			//更新入库明细表数量
			Integer inBoundQuantity = outBoundDetailsEntity.getInBoundQuantity();
			Integer quantity = outBoundDetailsEntity.getQuantity();
			if (inBoundQuantity < quantity) {
				throw new RuntimeException("商品库存数量不足!");
			}
			inBoundDetailsEntity = InBoundDetailsEntity.builder()
					.id(outBoundDetailsEntity.getInBoundDetailsId())
					.quantity(inBoundQuantity - quantity).build();
			if (inBoundDetailsMapper.update(inBoundDetailsEntity) <= 0) {
				throw new RuntimeException("更新入库明细表库存数量失败!");
			}
			//插入出库明细表
			outBoundDetailsEntity.setOutBoundId(outBoundEntity.getId());
			if (outBoundDetailsMapper.save(outBoundDetailsEntity) <= 0) {
				throw new RuntimeException("新增出库明细失败!");
			}
		}
		return CommonUtils.msg(countOutBound);
	}

	/**
     * 根据id查询
     * @param id
     * @return
     */
	@Override
	public R getOutBoundById(Long id) {
		OutBoundEntity outBound = outBoundMapper.getObjectById(id);
		return CommonUtils.msg(outBound);
	}

    /**
     * 修改
     * @param outBound
     * @return
     */
	@Override
	public R updateOutBound(OutBoundEntity outBound) {
		int count = outBoundMapper.update(outBound);
		return CommonUtils.msg(count);
	}

    /**
     * 删除
     * @param id
     * @return
     */
	@Override
	public R batchRemove(Long[] id) {
		int count = outBoundMapper.batchRemove(id);
		return CommonUtils.msg(id, count);
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public R batchRemoveLogic(Long[] id) {
		//更新出库主表的delete_flag
		int count1 = outBoundMapper.updateDeleteFlag(id);
		//出库主表插入作废的负记录
		int count2 = outBoundMapper.saveNegativeRecord(id);

		//出库明细表更新delete_flag
		int count3 = outBoundDetailsMapper.updateDeleteFlag(id);

		//更新入库明细表数量
		Map<String, Object> params = new HashMap<>();
		for (Long outBoundId:id){
			params.put("outBoundId",outBoundId);
			Query query = new Query(params);

			List<OutBoundDetailsEntity> list = outBoundDetailsMapper.list(query);

			InBoundDetailsEntity inBoundDetailsEntity;
			for (OutBoundDetailsEntity outBoundDetailsEntity : list) {

				//获取当前入库明细表的商品数量
				Integer inBoundQuantity = outBoundDetailsEntity.getInBoundQuantity();
				//获取待作废出库的商品数量
				Integer quantity = outBoundDetailsEntity.getQuantity();

				inBoundDetailsEntity = InBoundDetailsEntity.builder()
						.id(outBoundDetailsEntity.getInBoundDetailsId())
						.quantity(inBoundQuantity + quantity).build();
				if (inBoundDetailsMapper.backUpdate(inBoundDetailsEntity) <= 0) {
					throw new RuntimeException("更新入库明细表库存数量失败!");
				}
			}

		}

		return CommonUtils.msg(id, count1);
	}


}
